<?php


namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = [
        'delete_time',
        'main_img_id',
        'pivot',
        'from',
        'category_id',
        'create_time',
        'update_time',
        'summary'
    ];

    public function getMainImgUrlAttr($value,$data)
    {
        return $this->prefixImgUrl($value, $data);
    }

    public function imgs()
    {
        return $this->hasMany('ProductImage', 'product_id');
    }

    public function properties()
    {
        return $this->hasMany('ProductProperty', 'product_id', 'id');
    }

    public static function getMostRecent($count)
    {
        $products = self::order('create_time desc')->limit($count)->select();
        return $products;
    }

    public static function getProductsByCategoryID($categoryID)
    {
        $products = self::where('category_id', '=', $categoryID)->select();
        return $products;
    }

    public static function getProductDetail($id)
    {
//        $product = self::with(['imgs.imgUrl','properties'])->find($id);
        $product = self::with(['imgs.imgUrl'=>function($query){
            $query->order('order','desc');
        }, 'properties'])->find($id);
        return $product;
    }

}